Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

near-api-js

Package Overview
Dependencies
Maintainers
6
Versions
77
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

near-api-js

JavaScript library to interact with NEAR Protocol via RPC API

  • 0.44.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
66K
decreased by-2.91%
Maintainers
6
Weekly downloads
 
Created

What is near-api-js?

The near-api-js package is a JavaScript library for interacting with the NEAR blockchain. It provides tools for developers to build decentralized applications (dApps) on the NEAR platform, manage accounts, deploy smart contracts, and perform transactions.

What are near-api-js's main functionalities?

Account Management

This code demonstrates how to create and manage an account on the NEAR blockchain using near-api-js. It involves setting up a key store, generating a key pair, and connecting to the NEAR testnet.

const nearAPI = require('near-api-js');
const { connect, KeyPair, keyStores } = nearAPI;

async function createAccount() {
  const keyStore = new keyStores.InMemoryKeyStore();
  const keyPair = KeyPair.fromRandom('ed25519');
  await keyStore.setKey('testnet', 'example-account.testnet', keyPair);

  const near = await connect({
    networkId: 'testnet',
    keyStore,
    nodeUrl: 'https://rpc.testnet.near.org',
    walletUrl: 'https://wallet.testnet.near.org'
  });

  const account = await near.account('example-account.testnet');
  console.log(account);
}

createAccount();

Smart Contract Deployment

This code sample shows how to deploy a smart contract to the NEAR blockchain using near-api-js. It reads a compiled WebAssembly (WASM) file and deploys it to a specified account.

const nearAPI = require('near-api-js');
const { connect, keyStores } = nearAPI;
const fs = require('fs');

async function deployContract() {
  const keyStore = new keyStores.InMemoryKeyStore();
  const near = await connect({
    networkId: 'testnet',
    keyStore,
    nodeUrl: 'https://rpc.testnet.near.org'
  });

  const account = await near.account('example-account.testnet');
  const contractData = fs.readFileSync('./path-to-wasm-file.wasm');

  await account.deployContract(contractData);
  console.log('Contract deployed!');
}

deployContract();

Transaction Handling

This example demonstrates how to send a transaction on the NEAR blockchain using near-api-js. It involves sending a specified amount of NEAR tokens from one account to another.

const nearAPI = require('near-api-js');
const { connect, keyStores } = nearAPI;

async function sendTransaction() {
  const keyStore = new keyStores.InMemoryKeyStore();
  const near = await connect({
    networkId: 'testnet',
    keyStore,
    nodeUrl: 'https://rpc.testnet.near.org'
  });

  const account = await near.account('example-account.testnet');
  const result = await account.sendMoney('receiver-account.testnet', '1000000000000000000000000');
  console.log('Transaction result:', result);
}

sendTransaction();

Other packages similar to near-api-js

FAQs

Package last updated on 16 Dec 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc